package link.idns.g01.jdbcbridge.domain;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import lombok.Data;

@Data
public class MetaData {

    @Data
    public class MetaDataColumn {
        boolean autoIncrement;
        boolean caseSensitive;
        boolean searchable;
        boolean currency;
        int nullable;
        boolean signed;
        int displaySize;
        String label;
        String name;
        String schemaName;
        int precision;
        int scale;
        String tableName;
        String catalogName;
        int type;
        String typeName;
        boolean readOnly;
        boolean writable;
        boolean definitelyWritable;
        String className;
    }

    int count;

    List<MetaDataColumn> columns;

    public MetaData() {
        count = 0;
        columns = new ArrayList<>();
    }

    public MetaData(ResultSetMetaData metaData) throws SQLException {
        count = metaData.getColumnCount();

        List<MetaDataColumn> cols = new ArrayList<>();
        for (int i = 1; i <= count; i++) {
            MetaDataColumn col = new MetaDataColumn();
            col.autoIncrement = metaData.isAutoIncrement(i);
            col.caseSensitive = metaData.isCaseSensitive(i);
            col.searchable = metaData.isSearchable(i);
            col.currency = metaData.isCurrency(i);
            col.nullable = metaData.isNullable(i);
            col.signed = metaData.isSigned(i);
            col.displaySize = metaData.getColumnDisplaySize(i);
            col.label = metaData.getColumnLabel(i);
            col.name = metaData.getColumnName(i);
            col.schemaName = metaData.getSchemaName(i);
            col.precision = metaData.getPrecision(i);
            col.scale = metaData.getScale(i);
            col.tableName = metaData.getTableName(i);
            col.catalogName = metaData.getCatalogName(i);
            col.type = metaData.getColumnType(i);
            col.typeName = metaData.getColumnTypeName(i);
            col.readOnly = metaData.isReadOnly(i);
            col.writable = metaData.isWritable(i);
            col.definitelyWritable = metaData.isDefinitelyWritable(i);
            col.className = metaData.getColumnClassName(i);
            cols.add(col);
        }
        this.columns = cols;
    }
}
